/*
 * @lc app=leetcode.cn id=69 lang=javascript
 *
 * [69] x 的平方根 
 */

// @lc code=start
/**
 * @param {number} x
 * @return {number}
 */
let x = 4;
var mySqrt = function(x) {
  if (x === 0) return 0; // 0的算术平方根是0
  // 提示中 算术平方根范围为 0 <= result <= 46340 黄金分割法,猜大小
  let first = 0, last = Math.floor(x / 2), mid = null;
  while (first < last) {
    let mid = Math.floor((first + last) / 2);
    if (x === (mid * mid)) break;
    if (x >= (mid * mid)) {
      // mid - last
      first = mid;
    } else {
      // first - mid
      last = mid;
    }
    // 需要查到最接近的值
    
  }
  console.log(mid);
  return mid;
};
mySqrt(x);
// @lc code=end

